package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.addprem;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.InsurantModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.trail.TrialPersonModel;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import com.bmps.springcloud.insuredmicroservice.util.trial.TrialUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * description:
 *
 * @author jackdaw
 * @date 2020-06-28 14:25
 */
public class Hct010AddPremStrategy implements AddPremStrategy {
    @Override
    public TrialPersonModel calculateAddPrem(ProductModel productModel, RequestModel requestModel, RedisService redisService) {
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(productModel.getProdInsSeq(), insurantModelList);
        String riskCode = productModel.getProdCode();
        String payIntv = requestModel.getProductInfo().getPayIntv();

        TrialPersonModel trialPersonModel = new TrialPersonModel();
        trialPersonModel.setRiskCode(riskCode);
        trialPersonModel.setAppAge(DateUtil.getYears(insurantModel.getBirthday(), requestModel.getPolicyInfo().getPolApplyDate()));
        trialPersonModel.setSex(insurantModel.getSex());
        trialPersonModel.setPayEndYear(productModel.getPayYear());

        if (StringUtils.equals(UnderWriteConstant.MONTH_PAY, payIntv)) {
            trialPersonModel.setPayIntvRate("0.09");
        } else if (StringUtils.equals(UnderWriteConstant.SEASON_PAY, payIntv)) {
            trialPersonModel.setPayIntvRate("0.262");
        } else if (StringUtils.equals(UnderWriteConstant.HALF_YEAR_PAY, payIntv)) {
            trialPersonModel.setPayIntvRate("0.52");
        } else {
            trialPersonModel.setPayIntvRate("1");
        }
        // round(? Get ? / ? VPU ? * (? FirstScore ? -1), 2)
        Object object = redisService.get(AcceptConstant.REDIS_PREFIX_R004 + AcceptConstant.VERTICAL_BAR + riskCode);
        if (object == null) {
            return new TrialPersonModel();
        }
        TrialUtil.setSpecialElement(trialPersonModel, object.toString());
        double complex = productModel.getAmnt().doubleValue() / Double.valueOf(trialPersonModel.getVpu()) * productModel.getAddPremEM() / 100;
        trialPersonModel.setComplex(BigDecimal.valueOf(complex).setScale(2, BigDecimal.ROUND_HALF_UP));
        return redisService.getCalculateModeComplex(trialPersonModel, AcceptConstant.CALCULATION_TYPE_A);
    }
}
